﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using WorkflowWizart.Lib.Utility.Log;

namespace WorkflowWizart.Lib.Engine
{
    public class OperationCommandEngine
    {
        public static object RunCommand(string id, params object[] parameters)
        {
            Stopwatch timeCalculator = new Stopwatch();
            timeCalculator.Start();
            object result = null;
            try
            {
                var commandToRun = AppGlobals.OperationCommands.SingleOrDefault(c => c.Id == id);
                var method = Type.GetType(commandToRun.FullClassName).GetMethod(commandToRun.Name);
                result = method.Invoke(null, parameters);
            }
            catch (Exception ex)
            {
                LogHelper.AddCommandErrorLog(id, timeCalculator.ElapsedTicks, ex, parameters);
                return null;
            } 
            timeCalculator.Stop();
            LogHelper.AddCommandLog(id, timeCalculator.ElapsedTicks,string.Empty, parameters);
            
            timeCalculator = null;

            return result;
        }
    }
}
